using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class AttributesRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class AttributesDbo : DatabaseObject
        {
            public AttributesDbo()
            {
                Name = "Attributes";
                GeneratedClassName = "Attributes";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "AttributeTypeId", ColumnType = "smallint", IsIdentity = false, IsNullable = false, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "AttributeTypeId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "AttributeValue", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "AttributeValue" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "MinValue", ColumnType = "smallmoney", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "MinValue" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "MaxValue", ColumnType = "smallmoney", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "MaxValue" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CategoryId", ColumnType = "smallint", IsIdentity = false, IsNullable = true, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "CategoryId" });
              
            }
        }
		
		public static List<AttributeRow> Select(Expression<Func<AttributeRow, bool>> expression)
        {
            List<AttributeRow> rows = new List<AttributeRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<AttributeRow>(new AttributesDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<AttributeRow> Select()
		{
			List<AttributeRow> rows = new List<AttributeRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<AttributeRow>(new AttributesDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<AttributeRow> PagedSelect(Expression<Func<AttributeRow, bool>> filterExpression, Expression<Func<AttributeRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<AttributeRow> rows = new List<AttributeRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<AttributeRow>(new AttributesDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<AttributeRow> AttributeList)
        {
            AttributeRow row;

            while (reader.Read())
            {
                row = new AttributeRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					AttributeTypeId = Util.CastDbValue<int>(reader["AttributeTypeId"]),
					AttributeValue = Util.CastDbValue<string>(reader["AttributeValue"]),
					MinValue = Util.CastDbValue<decimal?>(reader["MinValue"]),
					MaxValue = Util.CastDbValue<decimal?>(reader["MaxValue"]),
					CategoryId = Util.CastDbValue<int?>(reader["CategoryId"])
                };
                                
                AttributeList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(AttributeRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<AttributeRow>(new AttributesDbo(), row);
            }
        }

		public static void Update(AttributeRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<AttributeRow>(new AttributesDbo(), row);
            }
        }

		public static void Delete(Expression<Func<AttributeRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<AttributeRow>(new AttributesDbo(), expression);
            }
        }

	}
}
	
